package com.ithao.controller;

import com.ithao.common.GlobalConst;
import com.ithao.entity.PageResult;
import com.ithao.entity.QueryPageBean;
import com.ithao.entity.Result;
import com.ithao.pojo.Course;
import com.ithao.pojo.User;
import com.ithao.service.CourseService;
import com.ithao.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @description ：学科控制器
 * @version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    /**
     * 添加学科
     * 获取表单数据
     * 初始化表单数据
     * 调用Service完成业务
     * 返回JSON到前端
     */
    @RequestMapping("add")
    public Result addCourse(@RequestBody Course course,HttpServletRequest request) {
        try {
            log.info("addCourse,{}", course);
            // 设置创建日期
            course.setCreateDate(DateUtils.parseDate2String(new Date()));
            // 获取当前用户的用户信息
            User user = (User) request.getSession().getAttribute(GlobalConst.SESSION_KEY_USER);
            if (user != null) {
                course.setUserId(user.getId());
            } else {
                // 调试时，默认是管理员
                course.setUserId(1);
            }
            courseService.addCourse(course);
            return new Result(true, "添加学科成功");
        } catch (RuntimeException e) {
            log.error("addCourse", e);
            return new Result(false, "添加学科失败," + e.getMessage());
        }
    }

    /**
     * 分页获取学科列表
     *
     */
    @RequestMapping("findListByPage")
    public Result findListByPage(@RequestBody QueryPageBean queryPageBean){
        if (queryPageBean == null) {
            queryPageBean = new QueryPageBean();
            queryPageBean.setCurrentPage(1);
            queryPageBean.setPageSize(10);
        }

        log.info("questionList queryPageBean:{}", queryPageBean);
        PageResult pageResult = courseService.findListByPage(queryPageBean);
       return new Result(true, "获取学科列表成功", pageResult);
    }

    @RequestMapping("update")
    public Result updateCourse(@RequestBody Course course){
        try {
            log.info("[Course Update]  courses:{}",course);
            courseService.updateCourse(course);
           return new Result(true, "更新学科成功");
        } catch (RuntimeException e) {
            log.error("updateCourse", e);
            return new Result(false, e.getMessage());
        }
    }

    @RequestMapping("delete")
    public Result deleteCourse(@RequestParam("courseId") Integer courseId){
        try {
            courseService.deleteCourse(courseId);
            return new Result(true, "删除学科成功");
        } catch (RuntimeException e) {
            log.error("deleteCourse", e);
            return new Result(false, e.getMessage());
        }
    }

    @RequestMapping("findListAll")
    public Result findListForQuestion(){
        try {
            List<Course> courseList = courseService.findListAll();
            return new Result(true, "获取列表成功", courseList);
        } catch (RuntimeException e) {
            log.error("findListForQuestion", e);
            return new Result(false, "获取列表失败");
        }
    }
}
